package com.mytools.tool.calendar;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eboss.sbs.R;
import com.mytools.tool.utils.ScreenUtils;
import com.mytools.tool.utils.StringUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class CalendarGridViewAdapter extends BaseAdapter {

    private Calendar calStartDate = Calendar.getInstance();
    private Calendar calSelected = Calendar.getInstance();

    public void setSelectedDate(Calendar cal) {
        calSelected = cal;
    }

    private Calendar calToday = Calendar.getInstance();
    private int iMonthViewCurrentMonth = 0;

    private void UpdateStartDateForMonth() {
        calStartDate.set(Calendar.DATE, 1);
        iMonthViewCurrentMonth = calStartDate.get(Calendar.MONTH);

        int iDay = 0;
        int iFirstDayOfWeek = Calendar.MONDAY;
        int iStartDay = iFirstDayOfWeek;
        if (iStartDay == Calendar.MONDAY) {
            iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY;
            if (iDay < 0)
                iDay = 6;
        }
        if (iStartDay == Calendar.SUNDAY) {
            iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
            if (iDay < 0)
                iDay = 6;
        }
        calStartDate.add(Calendar.DAY_OF_WEEK, -iDay);

        calStartDate.add(Calendar.DAY_OF_MONTH, -1);

    }

    ArrayList<Date> titles;

    private ArrayList<Date> getDates() {

        UpdateStartDateForMonth();

        ArrayList<Date> alArrayList = new ArrayList<Date>();

        for (int i = 1; i <= 42; i++) {
            alArrayList.add(calStartDate.getTime());
            calStartDate.add(Calendar.DAY_OF_MONTH, 1);
        }

        return alArrayList;
    }

    private Activity activity;
    
    Resources resources;

    // construct
    public CalendarGridViewAdapter(Activity a, Calendar cal) {
        calStartDate = cal;
        activity = a;
        resources = activity.getResources();
        titles = getDates();
    }

    public CalendarGridViewAdapter(Activity a) {
        activity = a;
        resources = activity.getResources();
    }


    @Override
    public int getCount() {
        return titles.size();
    }

    @Override
    public Object getItem(int position) {
        return titles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout iv = new LinearLayout(activity);
        iv.setOrientation(LinearLayout.VERTICAL);
        
        AbsListView.LayoutParams lp0 = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, ScreenUtils.dip2px(activity, 65)); //
        iv.setLayoutParams(lp0);
        
        iv.setId(position + 5000);
        
        iv.setGravity(Gravity.CENTER);
        iv.setOrientation(LinearLayout.VERTICAL);
        iv.setBackgroundColor(resources.getColor(R.color.white));

        Date myDate = (Date) getItem(position);
        Calendar calCalendar = Calendar.getInstance();
        calCalendar.setTime(myDate);

        final int iMonth = calCalendar.get(Calendar.MONTH);
        final int iDay = calCalendar.get(Calendar.DAY_OF_WEEK);

        iv.setBackgroundColor(resources.getColor(R.color.white));
        if (iDay == 7) {
        	if(iMonth == iMonthViewCurrentMonth)
        		iv.setBackgroundColor(resources.getColor(R.color.text_6));
        }
        else if (iDay == 1) {
        	if(iMonth == iMonthViewCurrentMonth)
        		iv.setBackgroundColor(resources.getColor(R.color.text_7));
        }
        
        TextView txtToDay = new TextView(activity);
        txtToDay.setGravity(Gravity.CENTER_HORIZONTAL);
        txtToDay.setPadding(0, 5, 0, 0);
        txtToDay.setTextSize(12);
        
        CalendarUtil calendarUtil = new CalendarUtil(calCalendar);
        txtToDay.setText(calendarUtil.toString());
        
        TextView txtDay = new TextView(activity);
        txtDay.setGravity(Gravity.CENTER_HORIZONTAL);
        txtDay.setTextSize(18);

        if (iMonth == iMonthViewCurrentMonth) {
            txtToDay.setTextColor(resources.getColor(R.color.Gray));
            txtDay.setTextColor(resources.getColor(R.color.DarkGray));
            txtDay.setTypeface(Typeface.DEFAULT,Typeface.BOLD);
            txtDay.setTextColor(0xff666666);
        } else {
            txtToDay.setTextColor(resources.getColor(R.color.noMonth));
            txtDay.setTextColor(resources.getColor(R.color.noMonth));
            txtDay.setTypeface(Typeface.DEFAULT,Typeface.NORMAL);
        }
        
        int day = myDate.getDate();
        txtDay.setText(String.valueOf(day));
        txtDay.setId(position + 500);
        iv.setTag(myDate);
        
        String solarMsg = new SolarTermsUtil(calCalendar).getSolartermsMsg();
        String gremessage = new GregorianUtil(calCalendar).getGremessage();
        if(!StringUtil.isNull(solarMsg)||!StringUtil.isNull(gremessage)){
        	if(iMonth == iMonthViewCurrentMonth){
        		txtToDay.setTextColor(resources.getColor(R.color.theme_color));
        		txtToDay.setTypeface(Typeface.DEFAULT,Typeface.BOLD);
        	}
        }
        
        if (equalsDate(calToday.getTime(), myDate)) {
            //iv.setBackgroundColor(resources.getColor(R.color.event_center));
            //txtDay.setTextSize(18); 
        	txtDay.setTextColor(resources.getColor(R.color.OrangeRed));
        	
        	txtToDay.setText("今日");
        	txtToDay.setTextColor(resources.getColor(R.color.OrangeRed));
        	txtToDay.setTypeface(Typeface.DEFAULT,Typeface.BOLD); //����
        } else {
        	//txtDay.setTextSize(18);
        }
        
        if (equalsDate(calSelected.getTime(), myDate)) {
            iv.setBackgroundResource(R.drawable.bg_calendar_selected);
            txtDay.setTextColor(resources.getColor(R.color.white));
            txtToDay.setTextColor(resources.getColor(R.color.white));
            
        } else {
            if (equalsDate(calToday.getTime(), myDate)) {
                //iv.setBackgroundColor(resources.getColor(R.color.calendar_zhe_day));
            }
        }
        
        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        iv.addView(txtDay, lp1);
        LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        iv.addView(txtToDay, lp2);
        
        return iv;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    private Boolean equalsDate(Date date1, Date date2) {
        if (date1.getYear() == date2.getYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate()) {
            return true;
        } else {
            return false;
        }
    }

}
